<?php
class RegisterAction extends Action {
	public function index() {
		$this->assign ( 'title', '羽毛球积分赛 - 新用户注册' );
		
		// 判断用户登录信息
		$this->assign ( 'admin', 'none' );
		if (session ( 'username' )) {
			$this->assign ( 'loginuser', session ( 'username' ) );
			$this->assign ( 'userscore', session ( 'userscore' ) );
			$this->assign ( 'usermatchcount', session ( 'usermatchcount' ) );
			$this->assign ( 'loginusercolor', get_color ( session ( 'usergender' ) ) );
			
			if (intval ( session ( 'userlevel' ) ) < 1) {
				$this->assign ( 'userstatus', '（未认证）' );
			} else {
				$this->assign ( 'userstatus', '' );
			}
			
			if (intval ( session ( 'userlevel' ) ) > 1) {
				$this->assign ( 'admin', 'inline' );
			}
			
			$this->assign ( 'logoffblock', 'inline' );
			$this->assign ( 'loginblock', 'none' );
		} else {
			$this->assign ( 'logoffblock', 'none' );
			$this->assign ( 'loginblock', 'inline' );
		}
		
		$this->display ();
	}
	
	public function verify() {
		import ( "ORG.Util.Image" );
		Image::buildImageVerify ();
	}
	
	public function add() {
		if (md5 ( $_POST ['verifyTest'] ) != session ( 'verify' )) {
			$this->error ( '验证码错误' );
		}
		
		$users = D ( 'Users' );
		
		if (! $users->create ()) {
			$this->error ( $users->getError () );
		}
		// dump($users);
		
		$users->register_time = date ( "Y-m-d H:i:s" );
		$users->password = md5 ( $users->password );
		$users->last_visit_time = date ( "Y-m-d H:i:s" );
		$users->last_visit_ip = get_client_ip ();
		//dump ( $users );
		
		if (! $users->add ()) {
			$this->error ( $users->getError () );
		}
		
		$this->success ( "恭喜你注册成功！暂时为受限状态，请耐心等待管理员审核~" );
	}
}